package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */
    @Override
    @Transactional
    public Result addCategory(CategoryDTO categoryDTO) {
        //非空判断
        if(categoryDTO == null){
            return Result.error("添加分类失败...");
        }

        Category category = new Category();
        BeanUtils.copyProperties( categoryDTO, category);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());

        //还有字段，就是分类的时候，给菜品的状态定义
        //默认是禁用的状态
        category.setStatus(StatusConstant.DISABLE);

        categoryMapper.addCategory(category);


        //调用mapper
        return Result.success();
    }


    /**
     * 分页查询菜品信息
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public Result<PageResult> pageCategory(CategoryPageQueryDTO categoryPageQueryDTO) {
        //边界判断
        if(categoryPageQueryDTO == null){
            return Result.error("菜品分页查询失败...");
        }
        //分页助手
        PageHelper.startPage(categoryPageQueryDTO.getPage() , categoryPageQueryDTO.getPageSize());

        //调用mapper，分页查询
        Page<Category> page = categoryMapper.pageQueryCategory(categoryPageQueryDTO);

        PageResult pageResult = new PageResult();
        pageResult.setRecords(page.getResult());
        pageResult.setTotal(page.getTotal());
        //返回值
        return Result.success(pageResult);
    }

    /**
     * 根据ID删除分类
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Result deleteByCategoryID(Integer id) {
        if(id == null){
            return Result.error("删除失败...");
        }
        /*      代码健壮性的问题，就是边界问题
                1、菜品分类删除的时候，需要查询一下，菜品表里有没有关联的，
                    如果有，就不能删除菜品分类
                    如果没有，才执行删除菜品分类
         *
         */


        categoryMapper.deleteByCategoryID(id);

        return Result.success();
    }

    /**
     * 修改分类名称
     *
     * @param categoryDTO
     * @return
     */
    @Override
    @Transactional
    public Result updateCategory(CategoryDTO categoryDTO) {
        if(categoryDTO == null){
            return Result.error("修改失败...");
        }

        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.updateCategory(category);


        return Result.success();
    }


    /**
     * @param status
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Result updateCategoryStatus(Integer status, Long id) {
        if (status == null || id == null){
            return Result.error("状态更新失败...");
        }

        Category category = Category.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build();

        categoryMapper.updateCategory(category);


        return Result.success();
    }

    /**
     * 条件查询
     *
     * @param type
     * @return
     */
    @Override
    public Result getCategoryList(Integer type) {

        List<Category> categoryList = categoryMapper.getCategoryList(type);

        return Result.success(categoryList);
    }
}
